package reflectDemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void reflectNewInstance(){
        try {
            Class<?> c1 = Class.forName("reflectDemo.Student");
            Student student = (Student) c1.newInstance();
            System.out.println();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor(){
        try {
            Class<?> c1 = Class.forName("reflectDemo.Student");
            Constructor <Student>constructor = (Constructor <Student>)c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Student student = constructor.newInstance("xing",20);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    // 反射私有属性
    public static void reflectPrivateField(){
        try {
            Class <?> c1 = Class.forName("reflectDemo.Student");
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);
            Student student = (Student) c1.newInstance();
            //field.set(student,"xing");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
         catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    // 反射私有方法
    public static void reflectPrivateMethod(){
        try {
            Class<?> c1 =  Class.forName("reflectDemo.Student");
            Method method = c1.getDeclaredMethod("study");
            method.setAccessible(true);
            Student student = (Student) c1.newInstance();
            method.invoke(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }
    public static void main1(String[] args) {
        try{
            Class<?> c1 = Class.forName("reflectDemo.Student");
            Class<?> c2 = Student.class;
            Student student1 = new Student();
            Class<?> c3 = student1.getClass();
            //class对象只有一个
            System.out.println(c1.equals(c2));
            System.out.println(c1.equals(c3));
            System.out.println(c2.equals(c3));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
